package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.bo.SpuBo;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.PrivateKey;
import java.util.List;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 调用方法查询数据。填充商品
     */

    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                           @RequestParam(value = "rows",defaultValue = "5") Integer rows,
                                                           @RequestParam(value = "key",required = false) String key,
                                                           @RequestParam(value = "saleable",required = false) Boolean saleable){

        PageResult<SpuBo> pageResults=this.goodsService.querySpuByPage(page,rows,key,saleable);
        if (pageResults==null||pageResults.getItems()==null||pageResults.getItems().size()==0){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(pageResults);

    }
    @PostMapping("goods")
    public ResponseEntity<Void> save(@RequestBody SpuBo spuBo){

        this.goodsService.saveGoods(spuBo);
        return ResponseEntity.ok().build();


    }


}
